package com.orientechnologies.tinkerpop;

import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.ODatabaseType;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.OrientDBConfig;
import org.apache.tinkerpop.gremlin.orientdb.OrientGraph;
import org.apache.tinkerpop.gremlin.orientdb.OrientGraphFactory;
import org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResultSet;
import org.junit.Assert;
import org.junit.Test;

/** Created by Enrico Risa on 14/03/17. */
public class OrientGraphOpenTest extends AbstractRemoteTest {

  @Test
  public void openRemoteTest() {
    OrientGraph graph = OrientGraph.open("remote:localhost/" + name.getMethodName());
    try {
      OGremlinResultSet resultSet = graph.executeSql("select from OUser");
      Assert.assertEquals(3, resultSet.stream().count());
    } finally {
      graph.close();
    }
  }

  @Test
  public void openFactoryRemoteTest() {
    OrientGraphFactory factory = new OrientGraphFactory("remote:localhost/" + name.getMethodName());
    OrientGraph graph = factory.getNoTx();
    try {
      OGremlinResultSet resultSet = graph.executeSql("select from OUser");
      Assert.assertEquals(3, resultSet.stream().count());
    } finally {
      graph.close();
      factory.close();
    }
  }

  @Test
  public void openFactoryRemoteWithOrientTest() {
    OrientDB orientDB =
        new OrientDB(
            "remote:localhost",
            "root",
            "root",
            OrientDBConfig.builder()
                .addConfig(OGlobalConfiguration.CREATE_DEFAULT_USERS, false)
                .build());
    orientDB.execute(
        "create database testWithOrientDB memory users(admin identified by 'admin' role admin)");
    OrientGraphFactory factory =
        new OrientGraphFactory(
            orientDB, "testWithOrientDB", ODatabaseType.MEMORY, "admin", "admin");
    OrientGraph graph = factory.getNoTx();
    try {
      OGremlinResultSet resultSet = graph.executeSql("select from OUser");
      Assert.assertEquals(1, resultSet.stream().count());
    } finally {
      graph.close();
      factory.close();
      orientDB.drop("testWithOrientDB");
    }
  }
}
